home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / lib / c / fgets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  3.0 KB  |  131 lines

  1. /* main ACE linked library module: buffered file functions.
  2. ** Copyright (C) 1998 David Benn
  3. ** 
  4. ** This program is free software; you can redistribute it and/or
  5. ** modify it under the terms of the GNU General Public License
  6. ** as published by the Free Software Foundation; either version 2
  7. ** of the License, or (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  
  18.        fgets: get at most n chars from iop (also returns string or NULL). 
  19.     fgetline: get at most n chars from iop but don't include the final '\n'.
  20.    fgetchars: get at most n chars from iop including '\n's.
  21.   fgetseqfld: get the next sequential field in the specified file.
  22.  
  23.    fgets() was taken from K&R, 2nd ed., pg 165
  24.    for use in db.lib
  25.  
  26.    fgetline(), fgetchars() and fgetseqfld() are variations on fgets().
  27.  
  28.    Date: 4th-6th January 1993
  29. */
  30.  
  31. #define EOF    (-1L)
  32. #define NULL    (0L)
  33.  
  34. char *fgets(s,n,iop)
  35. char *s;
  36. unsigned long n;
  37. unsigned long iop;
  38. {
  39. int  c;
  40. char *cs;
  41.  
  42.  cs = s;
  43.  while (--n > 0 && (c = fgetc(iop)) != EOF)
  44.      if ((*cs++ = c) == '\n')
  45.      break;
  46.  *cs = '\0';
  47.  return (c == EOF && cs == s) ? NULL : s;  
  48. }
  49.  
  50. void fgetline(s,n,iop)
  51. char *s;
  52. unsigned long n;
  53. unsigned long iop;
  54. {
  55. int  c;
  56. char *cs;
  57.  
  58.  cs = s;
  59.  while (--n > 0 && (c = fgetc(iop)) != EOF)
  60.      if ((*cs++ = c) == '\n')
  61.      break;
  62.  if (cs != s) --cs;  /* overwrite the '\n' */
  63.  *cs = '\0';
  64. }
  65.  
  66. void fgetchars(s,n,iop)
  67. char *s;
  68. unsigned long n;
  69. unsigned long iop;
  70. {
  71. int  c;
  72. char *cs;
  73.  
  74.  cs = s;
  75.  while (--n > 0 && (c = fgetc(iop)) != EOF) *cs++ = c;
  76.  *cs = '\0';
  77. }
  78.  
  79. void fgetseqfld(s,n,iop)
  80. char *s;
  81. unsigned long n;
  82. unsigned long iop;
  83. {
  84. int  c;
  85. char *cs;
  86.  
  87.  cs = s;
  88.  
  89.  --n;
  90.  
  91.  /* skip whitespace (incl. LF) and commas 
  92.     (latter from after a quote-delimited string) */
  93.  while ((c = fgetc(iop)) != EOF && (c <= ' ' || c == ','));
  94.  
  95.  /* quote-delimited string? */
  96.  if (n > 0 && c == '"')
  97.  {
  98.   do
  99.   {
  100.    c = fgetc(iop);
  101.    if (c != '"' && c != EOF) *cs++=c;
  102.   }
  103.   while (--n > 0 && c != '"' && c != EOF);
  104.  
  105.   *cs='\0';
  106.  
  107.   fgetc(iop);    /* - Assume next character is EOF, whitespace or comma. 
  108.            - The next test for EOF by ACE or this function will
  109.              be positive if we're now at EOF. This prevents ACE
  110.              from trying to read another line (say in a WHILE loop)
  111.              and getting NULL strings. 
  112.         */
  113.  
  114.   return;
  115.  }
  116.  
  117.  /* get the next field: could be a non-delimited string, integer or float */
  118.  while (n > 0 && c != EOF)
  119.  {
  120.      if ((*cs++ = c) <= ' ' || c == ',')  
  121.         break;    /* delimiters are: whitespace (incl. LF), comma */
  122.  
  123.      --n;
  124.  
  125.      c = fgetc(iop);
  126.  }
  127.  
  128.  if (cs != s) --cs;  /* overwrite the delimiter */
  129.  *cs = '\0';
  130. }
  131.